<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

	<!-- bootstarp-fileinput -->
	<link rel="stylesheet" type="text/css" href="../js/bootstrap-fileinput/css/fileinput.min.css"/>
	<script type="text/javascript" src="../js/bootstrap-fileinput/js/fileinput.min.js"></script>
	<script type="text/javascript" src="../js/bootstrap-fileinput/js/locales/zh.js"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
	<div class="box-header with-border">
		<h3 class="box-title">广告管理</h3>
	</div>

	<div class="box-body">

		<!-- 数据表格 -->
		<div class="table-box">

			<!--工具栏-->
			<div class="pull-left">
				<div class="form-group form-inline">
					<div class="btn-group">
						<button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
						<button type="button" onclick="deleteBatchs()" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
						<button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
						<button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
						<button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
					</div>
				</div>
			</div>
			<div class="box-tools pull-right">
				<div class="has-feedback">

				</div>
			</div>
			<!--工具栏/-->

			  <!--数据列表-->
			  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
				  <thead>
					  <tr>
						  <th class="" style="padding-right:0px">
							  <input id="selall" type="checkbox" class="icheckbox_square-blue">
						  </th>
						  <th class="sorting_asc">广告ID</th>
						  <th class="sorting">分类ID</th>
						  <th class="sorting">标题</th>
						  <th class="sorting">URL</th>
						  <th class="sorting">图片</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">是否有效</th>
						  <th class="text-center">操作</th>
					  </tr>
				  </thead>
				  <tbody id="content">

				  </tbody>
			  </table>
			  <!--数据列表/-->
		</div>
		<!-- 数据表格 /-->
	 </div>
	<!-- /.box-body -->

  	<script>
		$(function () {
			getData();
		})
		//品牌信息展示页面拼接
		function getData(){
			$.ajax({
				url:"/contentController/getContentInfos",
				type:"get",
				//data : $("#OneForm").serialize(),
				//data:{name:$("#namm").val()},
				dataType:"json",
				async:false,
				success:function(page){
					var result=page.data;
					console.log(result)
					var info = "";
					$(result).each(function (i,e) {
						info+= '<tr>';
						info+= '<td><input  type="checkbox" value="'+e["id"]+'"></td>';
						info+= '<td>'+e['id']+'</td>';
						info+= '<td>'+e['name']+'</td>';
						info+= '<td>'+e['title']+'</td>';
						info+= '<td>'+e['url']+'</td>';
						info+= '<td><img src="'+e["pic"]+'" width="100px" height="50px"></td>';
						info+= '<td>'+e['sortOrder']+'</td>';
						if (e['status'] == 1){
							info+= '<td>有效</td>';
						}
						if (e['status'] == 2){
							info+= '<td>无效</td>';
						}
						info+= '<td class="text-center">';
						info+= '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal2" onclick="updateContentInfos('+e["id"]+')"  >修改</button>';
						info+= '</td>';
						info+= '</tr>';
					})
					info += '</tbody>';
					info += '</table>';
					$("#content").html(info);
				},
				error:function(page){
					alert(page.message);
				}
			})
		}
	</script>
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select class="form-control" id="categoryId">
							<option th:each="c : ${contentCategories}" th:value="${c.id}">[[${c.name}]] </option>
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  id="title" class="form-control" placeholder="标题" >  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  id="url" class="form-control" placeholder="URL" >  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input  id="sortOrder" class="form-control" placeholder="排序" >  </td>
		      	</tr>			      	
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
								<!--<input type="file" id="file" />
					                <button class="btn btn-primary" type="button" >
				                   		上传
					                </button>	
					            </td>-->
									<input type="text" name="rimage" id="fileName">
									<label class="control-label" for="fileName">图片地址</label>
									<input type="file" id="myFile" name="image" multiple />
								<!--<td>
									<img  src="{{entity.pic}}" width="200px" height="100px">
								</td>-->
							</tr>						
						</table>
		      		</td>
		      	</tr>	      
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input id="status" type="checkbox"  class="icheckbox_square-blue" >
		      		</td>
		      	</tr>  	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="saveGontent()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
  <div class="modal fade" id="editModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel2">广告编辑</h3>
			  </div>
			  <div class="modal-body">
				  <input id="idd" type="hidden">
				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>广告分类</td>
						  <td>
							  <select class="form-control" id="categoryId2">
								  <option th:each="c : ${contentCategories}" th:value="${c.id}">[[${c.name}]] </option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>标题</td>
						  <td><input  id="title2" class="form-control" placeholder="标题" >  </td>
					  </tr>
					  <tr>
						  <td>URL</td>
						  <td><input  id="url2" class="form-control" placeholder="URL" >  </td>
					  </tr>
					  <tr>
						  <td>排序</td>
						  <td><input  id="sortOrder2" class="form-control" placeholder="排序" >  </td>
					  </tr>
					  <tr>
						  <td>广告图片</td>
						  <td>
							  <table >
								  <tr>
									  <td>
										  <input type="text" name="rimage" id="fileName2">
										  <label class="control-label" for="fileName">图片地址</label>
										  <input type="file" id="myFile2" name="image" multiple />
									  </td>
									  <td id="bbb">

									  </td>
								  </tr>
							  </table>
						  </td>
					  </tr>
					  <tr>
						  <td>是否有效</td>
						  <td>
							  <input id="status2" type="checkbox" value="1" class="icheckbox_square-blue" >
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" onclick="updateContentss()" data-dismiss="modal" aria-hidden="true">修改</button>
				  <button class="btn btn-default" onclick="reload()" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
  <!--保存-->
<script>
	function reload() {
		window.location.reload();
	}

	function saveGontent() {
		//广告分类
		var categoryId = $("#categoryId").val();
		//alert(categoryId)
		//标题
		var title = $("#title").val();
		//地址
		var url = $("#url").val();
		//图片
		var pic = $("#fileName").val();
		//排序
		var sortOrder = $("#sortOrder").val();
		//状态
		//var status = $("#status").val();
		var status="";
		if($("#status").is(":checked")){
			status="1"
		}else {
			status="2"
		}
		//.find("option:selected")
		$.ajax({
			url:"/contentController/insertContentss",
			type:"post",
			data:{categoryId:categoryId,title:title,url:url,pic:pic,sortOrder:sortOrder,status:status},
			dataType:"json",
			async:false,
			success:function(result){
				if (result.code == 200) {
					alert(result.message);
					window.location.reload();
				}
			},
			error:function(result){
				alert(result.message);
			}
		})
	}
	//回显
	function updateContentInfos(id) {
		$.ajax({
			url:"/contentController/updateContentInfos",
			type:"post",
			data:{id:id},
			dataType:"json",
			async:false,
			success:function(result){
				if (result.code == 200) {
					var contet = result.data;
					console.log(contet)
					$("#idd").val(contet.id)
					$("#categoryId2").val(contet.categoryId)
					$("#title2").val(contet.title)
					$("#url2").val(contet.url)
					$("#sortOrder2").val(contet.sortOrder)
					$("#title2").val(contet.title)

					var info = "";
					//info += '<tr><td><input type="text" name="rimage" id="fileName2">';
					//info +=	'<label class="control-label" for="fileName2">图片地址</label>';
					//info +=	'<input type="file" id="myFile2" name="image" multiple /></td>';
					info +=	'<input type="hidden" id="imgg" value="'+contet.pic+'"> <img src="'+contet.pic+'" width="200px" height="100px">';
					$("#bbb").html(info)
					var statu = contet.status;
					if ("1" == statu) {
						$("#status2").attr("checked",true);
					}else if ("2" == statu) {
						$("#status2").attr("checked",false);
					}

				}
			},
			error:function(result){
				alert(result.message);
			}
		})
	}
	//修改
	function updateContentss() {
		var id = $("#idd").val();
		//广告分类
		var categoryId = $("#categoryId2").val();
		//alert(categoryId)
		//标题
		var title = $("#title2").val();
		//地址
		var url = $("#url2").val();
		//图片
		var pic = null;
		if ($("#fileName2").val() != null && $("#fileName2").val() != ''){
			pic = $("#fileName2").val();
		} else{
			pic = $("#imgg").val()
		}
		//排序
		var sortOrder = $("#sortOrder2").val();
		var status="";
		if($("#status2").is(":checked")){
			status="1"
		}else {
			status="2"
		}
		$.ajax({
			url:"/contentController/updateContentss",
			type:"post",
			data:{id:id,categoryId:categoryId,title:title,url:url,pic:pic,sortOrder:sortOrder,status:status},
			dataType:"json",
			async:false,
			success:function(result){
				if (result.code == 200) {
					alert(result.message)
					window.location.reload();
				}
			},
			error:function(result){
				alert(result.message);
			}
		})
	}
	//广告批量删除
	function deleteBatchs() {
		var ids = [];
		var boxs = $("[type=checkbox]:checked");
		for (var i = 0; i < boxs.length; i++) {
			ids.push(boxs[i].value);
		}
		alert(ids);
		if(ids.length <= 0){
			toastr.error("请选择需要删除的数据");
			return;
		}
		if (confirm("是否确认删除?")){
			$.ajax({
				url:"/contentController/deleteBatchs",
				type:"post",
				data:{ids:ids},
				dataType:"json",
				async:true,
				success:function (result) {
					if (result.code == 200){
						alert(result.message);
						window.location.reload();
					}
				},
				error:function(result){
					alert(result.message);
				}
			})
		}
	}
</script>


  <!--上传图片-->
  <script type="text/javascript">
	  function fileInit(){
		  $('#myFile').fileinput({
			  language: 'zh',
			  uploadUrl: '/file/upload',
			  maxFileCount :3, // 最多同时传送几个图片，需要加multiple注解
			  enctype : 'multipart/form-data',
			  maxFileSize : 1024, //单位为kb
			  allowedFileExtensions : ['jpg', 'gif', 'png'],
			  browseClass: 'btn btn-primary'
		  });

		  $('#myFile').on('fileuploaded',function(event, data, previewId, index){
			  $("#fileName").val(data.response.imgUrl);
		  })
	  }

	  $(function(){
		  fileInit();
	  })
  </script>
  <!--上传图片2-->
  <script type="text/javascript">
	  function fileInit2(){
		  $('#myFile2').fileinput({
			  language: 'zh',
			  uploadUrl: '/file/upload',
			  maxFileCount :3, // 最多同时传送几个图片，需要加multiple注解
			  enctype : 'multipart/form-data',
			  maxFileSize : 1024, //单位为kb
			  allowedFileExtensions : ['jpg', 'gif', 'png'],
			  browseClass: 'btn btn-primary'
		  });

		  $('#myFile2').on('fileuploaded',function(event, data, previewId, index){
			  $("#fileName2").val(data.response.imgUrl);
		  })
	  }

	  $(function(){
		  fileInit2();
	  })
  </script>

</body>

</html>